//
//  PS_WebViewController.m
//  PolicySystem
//
//  Created by 丹丹 on 2018/10/30.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_WebViewController.h"
@interface PS_WebViewController ()<UIWebViewDelegate>{
    
    UIWebView *webView;
}

@end

@implementation PS_WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight-SafeAreaBottomHeight)];
    NSURL *url;

    //==1 banner ==2 协议  3 首页视频 4返利的产品详情 5信用卡申卡 6评级 7 首页网贷排名
    if (_isFrom == 2) {
        
        self.nav_titleLabel.text = @"注册协议";
        
    }else if(_isFrom == 1){
        self.nav_titleLabel.text = @"详情";
        
    }else if(_isFrom == 3){
        
        self.nav_titleLabel.text = @"视频";
        
    }else if(_isFrom == 4){
        
        self.nav_titleLabel.text = @"投资抢返利";
        
    }else if(_isFrom == 5){
        
        self.nav_titleLabel.text = @"信用卡申请";
        
    }else if(_isFrom == 6){
        
        self.nav_titleLabel.text = @"评级";
    }
    else if(_isFrom == 7){
        self.nav_titleLabel.text = @"网贷排名";
    }
    url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",_webUrl]];
    
    [webView sizeToFit];
    
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:webView];

    
    // Do any additional setup after loading the view.
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [self setWebViewHtmlImageFitPhone];
}

- (void)backButtonClick{
    
//    if (_isFrom == 3) {
//
//        if (webView.canGoBack) {
//
//            [webView goBack];
//
//        }else{
            [self.navigationController popViewControllerAnimated:YES];
//        }
//    }
}
- (void)setWebViewHtmlImageFitPhone
{
    CGFloat width = [[UIScreen mainScreen]bounds].size.width;
    
    NSString *jsStr = [NSString stringWithFormat:@"var script = document.createElement('script');"
                       "script.type = 'text/javascript';"
                       "script.text = \"function ResizeImages() { "
                       "var myimg,oldwidth;"
                       "var maxwidth = '%f';" //自定义宽度
                       "for(i=0;i <document.images.length;i++){"
                       "myimg = document.images[i];"
                       "if(myimg.width > maxwidth){"
                       "oldwidth = myimg.width;"
                       "myimg.width = maxwidth;"
                       "}"
                       "}"
                       "}\";"
                       "document.getElementsByTagName('head')[0].appendChild(script);",width];
    [webView stringByEvaluatingJavaScriptFromString:jsStr];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
